﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.Web.AppCode;
using System.Collections.Generic;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Cars : MgConfig
    {
        private Dictionary<int, Jy123.Model.Public_SportType> fatherDic = new Dictionary<int, Jy123.Model.Public_SportType>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Cars.aspx");
            BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            List<Model.Public_SportType> listSportType = BLLSportType.GetModelList(string.Empty);
            foreach (Model.Public_SportType sType in listSportType)
            {
                fatherDic.Add(sType.ID, sType);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Cars cars = e.Item.DataItem as Jy123.Model.Public_Cars;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = cars.ID.ToString();
                Label nameLabel = e.Item.FindControl("carsNameLabel") as Label;
                nameLabel.Text = cars.CName;
                Label levelLabel = e.Item.FindControl("carsLevelLabel") as Label;
                levelLabel.Text = cars.CLevel.Value.ToString();
                Label speedLabel = e.Item.FindControl("carsSpeedLabel") as Label;
                speedLabel.Text = cars.Speed.Value.ToString();
                Label stabilityLabel = e.Item.FindControl("carsStabilityLabel") as Label;
                stabilityLabel.Text = cars.Stability.Value.ToString();
                Label priceLabel = e.Item.FindControl("priceLabel") as Label;
                if (cars.Price.HasValue)
                {
                    priceLabel.Text = (Convert.ToInt32(cars.Price.Value)).ToString();
                }
                Label sLabel = e.Item.FindControl("sLabel") as Label;
                if (cars.FSportType > 0)
                {
                    sLabel.Text = fatherDic[cars.FSportType].SName;
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Cars BLLCars = new Jy123.BLL.Public_Cars();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Cars cars = BLLCars.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                carsNameTextBox.Text = cars.CName;
                carsLevelTextBox.Text = cars.CLevel.Value.ToString();
                carsSmallImage.Src = cars.SmallModel;
                carsBigImage.Src = cars.BigModel;
                carsSpeedTextBox.Text = cars.Speed.Value.ToString();
                carsStabilityTextBox.Text = cars.Stability.Value.ToString();
                if (cars.Price.HasValue)
                {
                    priceTextBox.Text = (Convert.ToInt32(cars.Price.Value)).ToString();
                }
                carsSmallImage.Visible = true;
                carsBigImage.Visible = true;
                if (cars.FSportType > 0)
                {
                    fatherDropDown.SelectedValue = cars.FSportType.ToString();
                }
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLCars.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Cars BLLCars = new Jy123.BLL.Public_Cars();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Cars cars = new Jy123.Model.Public_Cars();
                SetCars(cars);
                int carsId = BLLCars.Add(cars);
                cars.SmallModel = CreateLogo(carsId, CreateDir.SMALLCARS, fileUploadSmall.PostedFile);
                cars.BigModel = CreateLogo(carsId, CreateDir.BIGCARS, fileUploadBig.PostedFile);
                cars.ID = carsId;
                BLLCars.Update(cars);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Cars cars = BLLCars.GetModel(id);
                SetCars(cars);
                string smallModel = CreateLogo(id, CreateDir.SMALLCARS, fileUploadSmall.PostedFile);
                string bigModel = CreateLogo(id, CreateDir.BIGCARS, fileUploadBig.PostedFile);
                if (!string.IsNullOrEmpty(smallModel))
                {
                    cars.SmallModel = smallModel;
                }
                if (!string.IsNullOrEmpty(bigModel))
                {
                    cars.BigModel = bigModel;
                }
                BLLCars.Update(cars);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void SetCars(Jy123.Model.Public_Cars cars)
        {
            if (!string.IsNullOrEmpty(carsNameTextBox.Text))
            {
                cars.CName = carsNameTextBox.Text;
            }
            if (!string.IsNullOrEmpty(carsLevelTextBox.Text))
            {
                cars.CLevel = int.Parse(carsLevelTextBox.Text);
            }
            if (!string.IsNullOrEmpty(carsSpeedTextBox.Text))
            {
                cars.Speed = int.Parse(carsSpeedTextBox.Text);
            }
            if (!string.IsNullOrEmpty(carsStabilityTextBox.Text))
            {
                cars.Stability = int.Parse(carsStabilityTextBox.Text);
            }
            if (!string.IsNullOrEmpty(priceTextBox.Text))
            {
                cars.Price = decimal.Parse(priceTextBox.Text);
            }
            if (!string.IsNullOrEmpty(fatherDropDown.SelectedValue))
            {
                cars.FSportType = int.Parse(fatherDropDown.SelectedValue);
            }
        }

        private void ClearPage()
        {
            carsNameTextBox.Text = string.Empty;
            carsLevelTextBox.Text = string.Empty;
            carsSpeedTextBox.Text = string.Empty;
            carsStabilityTextBox.Text = string.Empty;
            actionTypeHidden.Value = "Add";
            carsSmallImage.Visible = false;
            carsBigImage.Visible = false;
            priceTextBox.Text = string.Empty;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Cars BLLCars = new Jy123.BLL.Public_Cars();
            List<Jy123.Model.Public_Cars> listCars = BLLCars.GetModelList(string.Empty);
            carsRepeater.DataSource = listCars;
            carsRepeater.DataBind();
            carsRepeater.Visible = listCars.Count > 0;

            BLL.Public_SportType BLLJType = new Jy123.BLL.Public_SportType();
            List<Model.Public_SportType> listJType = BLLJType.GetModelList("ID = 13 OR ID =14");

            fatherDropDown.DataSource = listJType;
            fatherDropDown.DataTextField = "SName";
            fatherDropDown.DataValueField = "ID";
            fatherDropDown.DataBind();
            fatherDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected string CreateLogo(int carsId, string type, HttpPostedFile postFile)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateIconPath(type, carsId, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
